﻿Public Class receiptTransaction
    Dim item As ListViewItem
    Sub listdata()
        If Not rs.EOF Then
            ListView1.Items.Clear()
            rs.MoveFirst()
            Do While Not rs.EOF
                item = ListView1.Items.Add(rs.Fields("productcode").Value)
                item.SubItems.Add(rs.Fields("quantity").Value)
                item.SubItems.Add(rs.Fields("description").Value)
                item.SubItems.Add(rs.Fields("sellingprice").Value)
                item.SubItems.Add(rs.Fields("Lineprice").Value)
                rs.MoveNext()

            Loop
        Else
            ListView1.Items.Clear()

        End If
        rs.Close()
        rs = Nothing

    End Sub
    Private Sub receiptTransaction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Module1.connect()
        TextBox10.Text = Date.Today
        TextBox7.Hide()
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        TextBox5.Enabled = False
        TextBox6.Enabled = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       rs = New ADODB.Recordset
        rs.Open("SELECT * from customers where customerno = " & TextBox4.Text & "", cn, 3, 2)
        If Not rs.EOF Then
            TextBox1.Text = rs.Fields("name1").Value
            TextBox2.Text = rs.Fields("address").Value
            TextBox3.Text = rs.Fields("city").Value
        End If
        rs.Close()
        rs = Nothing

       
        rs = New ADODB.Recordset
        rs.Open("select * FROM receipts", cn, 3, 2)
        rs.AddNew()

        rs.Fields("date1").Value = Date.Today
        rs.Fields("Customerno").Value = TextBox4.Text
        rs.Update()
        rs.Close()
        rs = Nothing

        Button1.Enabled = False
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = False
        TextBox5.Enabled = True
        TextBox6.Enabled = True
        Button7.Enabled = False


        rs = New ADODB.Recordset
        rs.Open("select max(receiptno) as e FROM receipts", cn, 3, 2)

        If Not rs.EOF Then
            TextBox8.Text = rs.Fields("e").Value
        End If
        rs.Close()
        rs = Nothing

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        rs = New ADODB.Recordset
        rs.Open("select max(receiptno) as e FROM receipts", cn, 3, 2)
        Dim receiptnum As Integer
        If Not rs.EOF Then
            receiptnum = rs.Fields("e").Value
        End If
        rs.Close()
        rs = Nothing

        rs = New ADODB.Recordset
        rs.Open("SELECT * from customers where customerno = " & TextBox4.Text & "", cn, 3, 2)
        If Not rs.EOF Then
            TextBox1.Text = rs.Fields("name1").Value
            TextBox2.Text = rs.Fields("address").Value
            TextBox3.Text = rs.Fields("city").Value
        End If
        rs.Close()
        rs = Nothing

        rs = New ADODB.Recordset
        rs.Open("SELECT * from receipts where receiptno = " & receiptnum & "", cn, 3, 2)
        rs.Fields("date1").Value = Date.Today
        rs.Fields("Customerno").Value = TextBox4.Text
        rs.Update()
        rs.Close()
        rs = Nothing



        
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        rs = New ADODB.Recordset
        rs.Open("select max(receiptno) as e FROM receipts", cn, 3, 2)
        Dim receiptnum As Integer
        If Not rs.EOF Then
            receiptnum = rs.Fields("e").Value
        End If
        rs.Close()
        rs = Nothing

        rs = New ADODB.Recordset
        rs.Open("select unitprice FROM products where productcode = '" & TextBox6.Text & "'", cn, 3, 2)
        Dim sellingprce As Double
        If Not rs.EOF Then
            sellingprce = rs.Fields("unitprice").Value
        End If
        rs.Close()
        rs = Nothing

        rs = New ADODB.Recordset
        rs.Open("select * FROM receiptproducts", cn, 3, 2)
        rs.AddNew()

        rs.Fields("receiptno").Value = receiptnum
        rs.Fields("productcode").Value = TextBox6.Text
        rs.Fields("quantity").Value = TextBox5.Text
        rs.Fields("sellingprice").Value = sellingprce
        rs.Update()
        rs.Close()
        rs = Nothing

        Button6.Enabled = True

        rs = New ADODB.Recordset
        rs.Open("SELECT quantity, productcode, description, sellingprice, (quantity*sellingprice) AS Lineprice FROM Query5 WHERE receiptno = " & receiptnum & "", cn, 3, 2)
        Me.listdata()

        rs = New ADODB.Recordset
        rs.Open("SELECT SUM(quantity*sellingprice) AS total FROM Query5 WHERE receiptno = " & receiptnum & "", cn, 3, 2)

        If Not rs.EOF Then
            Label11.Text = rs.Fields("total").Value
        End If
        rs.Close()
        rs = Nothing
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        rs = New ADODB.Recordset
        rs.Open("select max(receiptno) as e FROM receipts", cn, 3, 2)
        Dim receiptnum As Integer
        If Not rs.EOF Then
            receiptnum = rs.Fields("e").Value
        End If
        rs.Close()
        rs = Nothing


        rs = New ADODB.Recordset
        rs.Open("select * FROM receiptproducts where receiptno = " & receiptnum & " AND productcode = '" & TextBox7.Text & "' AND quantity = " & TextBox5.Text & "", cn, 3, 2)
        rs.Delete()
        rs.Close()
        rs = Nothing

      
        rs = New ADODB.Recordset
        rs.Open("SELECT quantity, productcode, description, sellingprice, (quantity*sellingprice) AS Lineprice FROM Query5 WHERE receiptno = " & receiptnum & "", cn, 3, 2)
        Me.listdata()

        rs = New ADODB.Recordset
        rs.Open("SELECT SUM(quantity*sellingprice) AS total FROM Query5 WHERE receiptno = " & receiptnum & "", cn, 3, 2)


       
        If Not rs.EOF Then
            Label11.Text = rs.Fields("total").Value.ToString
            End If

        rs.Close()
        rs = Nothing
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        TextBox7.Text = ListView1.FocusedItem.Text
    End Sub


    Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
        rs = New ADODB.Recordset
        rs.Open("select max(receiptno) as e FROM receipts", cn, 3, 2)
        Dim receiptnum As Integer
        If Not rs.EOF Then
            receiptnum = rs.Fields("e").Value
        End If
        rs.Close()
        rs = Nothing

        rs = New ADODB.Recordset
        rs.Open("select productcode,quantity, description, sellingprice, (quantity*sellingprice) AS Lineprice FROM Query5 WHERE productcode = '" & TextBox7.Text & "' AND receiptno = " & receiptnum & " ", cn, 3, 2)
        If Not rs.EOF Then
            TextBox6.Text = rs.Fields("productcode").Value
            TextBox5.Text = rs.Fields("quantity").Value

        End If
        rs.Close()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        rs = New ADODB.Recordset
        rs.Open("select max(receiptno) as e FROM receipts", cn, 3, 2)
        Dim receiptnum As Integer
        If Not rs.EOF Then
            receiptnum = rs.Fields("e").Value
        End If
        rs.Close()
        rs = Nothing

        rs = New ADODB.Recordset
        rs.Open("select * FROM receipts where receiptno = " & receiptnum & "", cn, 3, 2)
        rs.Delete()
        rs.Close()
        rs = Nothing

       

        MainScreen.Show()
        Me.Hide()

    End Sub

   
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    End Sub

    Private Sub PrintDocument1_PrintPage_1(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        Button1.Hide()
        Button2.Hide()
        Button3.Hide()
        Button4.Hide()
        Button5.Hide()
        Button6.Hide()
        Button7.Hide()
        Label6.Hide()
        Label7.Hide()
        TextBox5.Hide()
        TextBox6.Hide()
        On Error GoTo errorfound

        PrintForm1.Print()

        MainScreen.Show()
        Me.Close()

        Exit Sub

errorfound: MsgBox("error")
        Resume Next


    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        MainScreen.Show()
        Me.Close()
    End Sub
End Class